package com.ego.coupon.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ego.common.constant.CouponConstant;
import com.ego.common.util.PageUtil;
import com.ego.common.vo.PageVo;
import com.ego.coupon.po.SeckillSkuRelation;
import com.ego.coupon.service.SeckillSkuRelationService;
import com.ego.coupon.vo.SeckillSearchVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.ego.coupon.dao.SeckillSessionDao;
import com.ego.coupon.po.SeckillSession;
import com.ego.coupon.service.SeckillSessionService;
import org.springframework.util.CollectionUtils;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: ego
 * @description 秒杀活动场次对应实现类
 * @author KamTang
 * @date 2022-11-13 13:17:12
 */
@Service("seckillSessionService")
public class SeckillSessionServiceImpl extends ServiceImpl<SeckillSessionDao, SeckillSession> implements SeckillSessionService {

    @Autowired
    private SeckillSkuRelationService seckillSkuRelationService;

    @Override
    public PageVo<SeckillSession> search(SeckillSearchVo search) {
        LambdaQueryWrapper<SeckillSession> wrapper = Wrappers.<SeckillSession>lambdaQuery()
                .like(StringUtils.isNotEmpty(search.getKeyword()), SeckillSession::getName, search.getKeyword());
        Page<SeckillSession> page = baseMapper.selectPage(Page.of(search.getPageNum(), search.getPageSize()), wrapper);
        return PageUtil.toPage(page);
    }

    @Override
    public List<SeckillSession> getLastDaySession() {
        List<SeckillSkuRelation> seckillSkuRelations = seckillSkuRelationService.list();
        if (!CollectionUtils.isEmpty(seckillSkuRelations)) {
            Map<Long, List<SeckillSkuRelation>> sessionIdWithRelationMap = seckillSkuRelations.stream()
                    .collect(Collectors.groupingBy(SeckillSkuRelation::getPromotionSessionId, Collectors.toList()));

            // 计算最近一天的活动
            List<SeckillSession> sessions = this.list(new QueryWrapper<SeckillSession>()
                    .between("start_time", startTime(), endTime())
                    .eq("status", CouponConstant.SessionStatusEnum.ENABLE.getCode()));
            Map<Long, List<SeckillSession>> sessionIdWithSessionMap = sessions.stream()
                    .collect(Collectors.groupingBy(SeckillSession::getId, Collectors.toList()));

            // 如果有活动就遍历
            if (!sessionIdWithSessionMap.values().isEmpty()) {
                return sessionIdWithSessionMap.keySet().stream()
                        .map(sessionId -> {
                            List<SeckillSession> sessionPos = sessionIdWithSessionMap.get(sessionId);
                            sessionPos.forEach(session -> session.setSkuRelations(sessionIdWithRelationMap.get(sessionId)));
                            return sessionPos;
                        }).flatMap(Collection::stream).collect(Collectors.toList());
            }

        }
        return null;
    }

    private String startTime() {
        LocalDate now = LocalDate.now();
        LocalTime min = LocalTime.MIN;
        LocalDateTime today = LocalDateTime.of(now, min);
        return today.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }

    private String endTime() {
        LocalDate now = LocalDate.now();
        LocalDate localDate = now.plusDays(1);
        LocalDateTime lastDay = LocalDateTime.of(localDate, LocalTime.MAX);
        return lastDay.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
    }
}
